﻿//纹理、图片工具
using UnityEngine;
using System.Collections;
using System.IO;
using System;

public class imgTools {

    //双线性插值缩放图片,输入原始图片和缩放值，返回texture2d
    public static Texture2D ScaleTextureBilinear(Texture2D originalTexture, float scaleFactor) {
        Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalTexture.width * scaleFactor),
            Mathf.CeilToInt(originalTexture.height * scaleFactor));
        float scale = 1.0f / scaleFactor;
        int maxX = originalTexture.width - 1;
        int maxY = originalTexture.height - 1;
        for (int y = 0; y < newTexture.height; y++) {
            for (int x = 0; x < newTexture.width; x++) {
                // Bilinear Interpolation  
                float targetX = x * scale;
                float targetY = y * scale;
                int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
                int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
                int x2 = Mathf.Min(maxX, x1 + 1);
                int y2 = Mathf.Min(maxY, y1 + 1);

                float u = targetX - x1;
                float v = targetY - y1;
                float w1 = (1 - u) * (1 - v);
                float w2 = u * (1 - v);
                float w3 = (1 - u) * v;
                float w4 = u * v;
                Color color1 = originalTexture.GetPixel(x1, y1);
                Color color2 = originalTexture.GetPixel(x2, y1);
                Color color3 = originalTexture.GetPixel(x1, y2);
                Color color4 = originalTexture.GetPixel(x2, y2);
                Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
                    Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
                    Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
                    Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
                    );
                newTexture.SetPixel(x, y, color);
            }
        }
        return newTexture;
    }
    //从某像机得到纹理,输入一个相机和矩形，返回一个texture2d(即把某相机所能看到的渲染到一张纹理上)
    public static Texture2D getCamRender(Camera cam,Rect rect) {
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        cam.targetTexture = rt;
        cam.Render();
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height,TextureFormat.ARGB32,false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        cam.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(rt);
        return screenShot;
    }
    //保存图片
    //从Texture2D保存
    public static string SaveImg(Texture2D tex,string path = ""){
        string combPath = "";
        if (path == "")
            path = Application.persistentDataPath;
        int count = 0;
        do {
            combPath = path + "/" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + (count == 0 ? "" : ("(" + count + ")")) + ".png";
            count++;
        } while (File.Exists(combPath));
        FileStream fs = null;
        try {
            fs = new FileStream(combPath, FileMode.Create);
            byte[] s = tex.EncodeToPNG();
            fs.Write(s, 0, s.Length);
        } catch (Exception e) {
            Debug.Log(e.ToString());
            combPath = "";
        } finally {
            if (fs != null)
                fs.Close();
        }
        return combPath;
    }
    //从Color[]保存
    public static string SaveImg(Color[] Pixels,int w,int h,string path = "") {
        if (Pixels.Length != w * h)
            return "";
        Texture2D tmp_tex = new Texture2D(w,h);
        return SaveImg(tmp_tex, path);
    }
}
